﻿using Common.Attributes;
using MODEL.ViewModel;
using QGZX.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace QGZX.Logic.Admin
{
    public  class DepartmentController : Controller
    {
        #region 部门列表（也是角色列表） 视图 + ActionResult index()
        /// <summary>
        /// 部门列表（也是角色列表） 视图
        /// </summary>
        /// <returns></returns>
        public ActionResult index()
        {
            return View();
        } 
        #endregion

        #region  部门列表列表 数据 +GetDepartmentData()
        [HttpPost]
        [AjaxRequest]
        /// <summary>
        /// 部门列表列表 视图
        /// </summary>
        /// <returns></returns>
        public ActionResult GetDepartmentData()
        {
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            var list = OperateContext.Current.BLLSession.Iqgzx_departmentBLL.GetPagedList(pageIndex, pageSize, x => x.Id != null, m => m.Id).Select(x => x.ToPOCO());
            int rowCount = OperateContext.Current.BLLSession.Iqgzx_departmentBLL.GetListBy(x => x.Id != null).Count();
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion

        #region 加载所有权限树 + GetDepartTree()
        /// <summary>
        /// 加载所有权限树
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetDepartTree(int id)
        {
            //获取角色权限
            var listAdminPer = OperateContext.Current.GetPermissionByDeparId(id);
            //获取所有权限
            var listAllPer = OperateContext.Current.BLLSession.Iqgzx_permissionBLL.GetListBy(x => x.PId != null).ToList();
            //获取父权限集合
            var listParentPer = (from x in listAllPer where x.PParent == 1 select x).ToList();

            //准备一个角色id，传给视图
            ViewBag.DepartId = id;

            return PartialView(new DepartmentTree { AdminPer = listAdminPer, AllPer = listAllPer, AllPartnetPer=listParentPer });
        } 
        #endregion

        #region 修改部门（角色）具有的权限 + SetDepartPer()
        /// <summary>
        /// 修改部门（角色）具有的权限 + SetDepartPer()
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult SetDepartPer()
        {
            //获取要修改权限的部门id
            int DepartId = int.Parse(Request.Form["rid"]);
            //获取新权限 的 id字符串
            string perIds = Request.Form["perid"];
            perIds = perIds + "|1";
            //查到模型
            MODEL.qgzx_department model = OperateContext.Current.BLLSession.Iqgzx_departmentBLL.GetListBy(x => x.Id == DepartId).First();
            //设置模型的权限字符串字段的值   并存到对应department表的权限字段去
            model.PermissionId = perIds;
            //--------------------------------------------------------为什么Modify不行，一定要ModifyBy
            int res = OperateContext.Current.BLLSession.Iqgzx_departmentBLL.ModifyBy(model, p => p.Id== DepartId, "Remark", "PermissionId");
            if (res > 0)
            {
                return OperateContext.Current.RedirectAjax("ok", "修改成功", null, "");
            }
            else
            {
                return OperateContext.Current.RedirectAjax("err", "出错了", null, "");
            }

        } 
        #endregion

        #region 加载修改部门 window 的html + EditDepartment()
        /// <summary>
        /// 加载修改部门 window 的html
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult EditDepartment(int id)
        {
            //根据id 查询要修改的部门
            var model = OperateContext.Current.BLLSession.Iqgzx_departmentBLL.GetListBy(x => x.Id == id).FirstOrDefault().ToViewModel();
            //将权限对象 传给视图的model属性
            return PartialView(model);
        }
        #endregion

        #region post修改部门 EditDepartment(MODEL.qgzx_department model)
        /// <summary>
        /// post修改部门 EditDepartment(MODEL.qgzx_department model)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult EditDepartment(MODEL.qgzx_department model)
        {
            int res = OperateContext.Current.BLLSession.Iqgzx_departmentBLL.Modify(model, "Name", "Remark", "PermissionId");
            if (res > 0)
            {
                return Redirect("/admin/Department/index");
            }
            return Redirect("/admin/Department/index");
        }
        #endregion

        #region 在这新增部门 html代码 + AddDepartment()
        /// <summary>
        /// 在这新增部门 html代码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult AddDepartment()
        {
            return PartialView("EditDepartment");
        }
        #endregion

        #region 在这新增部门 html代码 + AddDepartment
        /// <summary>
        /// 在这新增权限 html代码
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult AddDepartment(MODEL.qgzx_department model)
        {
            

            int res = OperateContext.Current.BLLSession.Iqgzx_departmentBLL.Add(model);

            if (res > 0)
            {
                return Redirect("/admin/Department/index?ok");
            }
            return Redirect("/admin/Department/index?err");
        }
        #endregion

        #region 删除部门 + DelDepartment()
        /// <summary>
        /// 删除权限 + DelPermission()
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult DelDepartment()
        {
            try
            {
                int id = int.Parse(Request.Form["id"]);
                OperateContext.Current.BLLSession.Iqgzx_departmentBLL.DelBy(x => x.Id == id);
                return OperateContext.Current.RedirectAjax("ok", "删除成功~", null, "");
            }
            catch (Exception ex)
            {

                return OperateContext.Current.RedirectAjax("err", "您现在删除的权限正在被使用,请先取消角色中的这个权限", null, "");
            }
        }
        #endregion

        #region 高级搜索(比较笨，先用这个方法) + ActionResult Search()
        /// <summary>
        /// 高级搜索(比较笨，先用这个方法)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Search()
        {
            string Name = Request.QueryString["Name"];
            string Remark = Request.QueryString["Remark"];
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            //高级搜索的功能！！！
            var list = OperateContext.Current.BLLSession.Iqgzx_departmentBLL.GetListBy(x => x.Id != null).ToList().Select(x=>x.ToPOCO());
            if (Name != "")
            {
                list = list.Where(x => x.Name.Contains(Name)).ToList();
            }
            if (Remark != "")
            {
                list = list.Where(x => x.Remark.Contains(Remark)).ToList();
            }
            int rowCount = list.Count();
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion
    }
}
